/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyCalculator;

import java.util.ArrayList;

/**
 *
 * @author fire
 */
public class PloyCalc {

    PloyCalc() {
    }
    //Calculate the Poly points
    public double[] CalcthePoly(double[][] inPoly, double lower, double upper, int capcity) {
        //inPoly[j][0]=Coefficient
        //inPoly[j][1]=power
        double delta_x = (upper - lower) / capcity;
        double[] a = new double[capcity];
        int i = 0, l = inPoly.length;
        //a[0] = 0;
        for (i = 0; i < capcity; i++) {
            double temp=0;
            double x = lower + i * delta_x;
            for (int j = l-1; j >= 0; j--) {
                temp+= inPoly[j][0] * Math.pow(x, inPoly[j][1]);
            }
            a[i]=temp;
        }
        return a;
    }
    //Find the root
    public double[] rootFinding(double[] CalcthePoly,double[][] inPoly, double lower, double upper,int capcity){
        double delta_x = (upper - lower) / capcity;

        ArrayList <Double>tempPloy=new ArrayList<Double>();


        //add for adding a new element
        //tempPloy.add(1.0);
        for(int i=0;i<CalcthePoly.length-1;i++){
            double temp1=CalcthePoly[i];
            double temp2=CalcthePoly[i+1];
            if(temp1<0&&temp2>0){
                double tempF=0;
                double mid=0;
                double a=lower+i*delta_x,b=lower+(i+1)*delta_x;
                while(b-a>0.001){
               mid=(a+b)/2;
               tempF=0;
                       for (int j = inPoly.length-1; j >= 0; j--){
                           tempF+= inPoly[j][0] * Math.pow(mid, inPoly[j][1]);
                       }
                           if(tempF<0){
                               a=mid;
                           }else if(tempF>0){
                               b=mid;
                            }else{
                   tempPloy.add(mid);
                   break;
                            }
                }
                if(tempF!=0){
                tempPloy.add(mid);}
            }
                else if(temp1>0&&temp2<0){
                    double tempF=0;
                double mid=0;
                double a=lower+i*delta_x,b=lower+(i+1)*delta_x;
                while(b-a>0.001){
               mid=(a+b)/2;
               tempF=0;
                       for (int j = inPoly.length-1; j >= 0; j--){
                           tempF+= inPoly[j][0] * Math.pow(mid, inPoly[j][1]);
                       }
                           if(tempF<0){
                               b=mid;
                           }else if(tempF>0){
                               a=mid;
                            }else{
                   tempPloy.add(mid);
                   break;
                            }
                }
                if(tempF!=0){
                tempPloy.add(mid);
                }
                }else if(temp1==0){
                    tempPloy.add(lower+i*delta_x);
                }else if(temp2==0){
                    tempPloy.add(lower+(i+1)*delta_x);
                    i+=1;
                }
        }




        tempPloy.size();
        double[] out = new double[tempPloy.size()];
        for (int count=0;count<tempPloy.size();count++){
            out[count]=tempPloy.get(count);
        }

        return out;
    }
}
